<template>
  <el-card class="form-container" shadow="never">
    <el-form :model="productCate"
             :rules="rules"
             ref="productCateFrom"
             label-width="150px">
      <el-form-item label="名称：" prop="name">
        <el-input v-model="productCate.name"></el-input>
      </el-form-item>
      <el-form-item label="计费类型：">
        <el-radio-group v-model="productCate.chargeType">
          <el-radio :label="0">按重量</el-radio>
          <el-radio :label="1">按件数</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="首重kg：">
        <el-input v-model="productCate.firstWeight"></el-input>
      </el-form-item>
      <el-form-item label="首费（元）：">
        <el-input v-model="productCate.firstFee"></el-input>
      </el-form-item>

      <el-form-item label="剩下的单价：">
        <el-input v-model="productCate.continmeFee"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit('productCateFrom')">提交</el-button>
        <el-button v-if="!isEdit" @click="resetForm('productCateFrom')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
  import {fetchList, createFeightTemplate, updateFeightTemplate, getFeightTemplate} from '@/api/pms/feightTemplate';


  const defaultFeightTemplate = {
    description: '',
    icon: '',
    keywords: '',
    name: '',
    navStatus: 0,
    parentId: 0,
    productUnit: '',
    showStatus: 0,
    indexStatus:0,
    sort: 0,
    productAttributeIdList: []
  };
  export default {
    name: "FeightTemplateDetail",
    props: {
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        productCate: Object.assign({}, defaultFeightTemplate),
        selectFeightTemplateList: [],
        rules: {

        },
        filterAttrs: [],
        filterProductAttrList: [{
          value: []
        }]
      }
    },
    created() {
      if (this.isEdit) {
        getFeightTemplate(this.$route.query.id).then(response => {
          this.productCate = response.data;
        });

      } else {
        this.productCate = Object.assign({}, defaultFeightTemplate);
      }
    },
    methods: {


      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm('是否提交数据', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              if (this.isEdit) {

                updateFeightTemplate(this.$route.query.id, this.productCate).then(response => {
                  this.$message({
                    message: '修改成功',
                    type: 'success',
                    duration: 1000
                  });
                  this.$router.back();
                });
              } else {
                createFeightTemplate(this.productCate).then(response => {

                  this.resetForm(formName);
                  this.$message({
                    message: '提交成功',
                    type: 'success',
                    duration: 1000
                  });
                });
              }
            });

          } else {
            this.$message({
              message: '验证失败',
              type: 'error',
              duration: 1000
            });
            return false;
          }
        });
      },
      resetForm(formName) {

        this.productCate = Object.assign({}, defaultFeightTemplate);

      },
      removeFilterAttr(productAttributeId) {
        if (this.filterProductAttrList.length === 1) {
          this.$message({
            message: '至少要留一个',
            type: 'warning',
            duration: 1000
          });
          return;
        }
        var index = this.filterProductAttrList.indexOf(productAttributeId);
        if (index !== -1) {
          this.filterProductAttrList.splice(index, 1)
        }
      },
      handleAddFilterAttr() {
        if (this.filterProductAttrList.length === 3) {
          this.$message({
            message: '最多添加三个',
            type: 'warning',
            duration: 1000
          });
          return;
        }
        this.filterProductAttrList.push({
          value: null,
          key: Date.now()
        });
      }
    },
    filters: {
      filterLabelFilter(index) {
        if (index === 0) {
          return '筛选属性：';
        } else {
          return '';
        }
      }
    }
  }
</script>

<style scoped>

</style>
